import axios from "axios";
import { createSlice } from "@reduxjs/toolkit";

const userStore = createSlice({
    name: "user",
    initialState: {
        rolePermissions: [],
        permissions: "",
    },
    reducers: {
        setRolePermissions(state, action) {
            state.rolePermissions = action.payload
        },
        setRole(state, action) {
            state.permissions = action.payload
        }
    }
})

const { setRolePermissions } = userStore.actions

const fetchRolePermissions = () => {
    return async (dispatch) => {
        const res = await axios.get('http://localhost:3004/rolePermissions')
        dispatch(setRolePermissions(res.data))
    }
}

export { fetchRolePermissions }

const storeReducer = userStore.reducer
export default storeReducer
